using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    processProductType pProductType;
    processProduct pProduct;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
            pProductType = new processProductType();
            pProduct = new processProduct();
            dtlProductType.DataSource = pProductType.getProductTypes().DefaultView;
            dtlProductType.DataKeyField = "ProductTypeID";
            dtlProductType.DataBind();
            PlaceHolderLogIn.Controls.Clear();

            if (Session["UserName"] == null || Session["UserName"].ToString().Equals(""))
            {
                Control crtl1 = Page.LoadControl("HeaderLogIn.ascx");
                PlaceHolderLogIn.Controls.Add(crtl1);
            }
            else
            {
                Control crtl1 = Page.LoadControl("HeaderLogOut.ascx");
                PlaceHolderLogIn.Controls.Add(crtl1);
            }
        
            PlaceHolderUser.Controls.Clear();
            if (Request["subpara"] != null)
            {
                string subpara = Request["subpara"].ToString();
                if (subpara.Equals("product"))
                {
                    Control crtl = Page.LoadControl("Product.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("newproduct"))
                {
                    Control crtl = Page.LoadControl("NewProduct.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("faq"))
                {
                    Control crtl = Page.LoadControl("FAQs.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("feedback"))
                {
                    Control crtl = Page.LoadControl("Feedback.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("createaccount"))
                {
                    Control crtl = Page.LoadControl("Register.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("productdetail"))
                {
                    Control crtl = Page.LoadControl("ProductDetail.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("addtocart"))
                {
                    Control crtl = Page.LoadControl("AddtoCart.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("login"))
                {
                    Control crtl = Page.LoadControl("LogIn.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("myaccount"))
                {
                    Control crtl = Page.LoadControl("MyAccount.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("search"))
                {
                    Control crtl = Page.LoadControl("Search.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("checkout"))
                {
                    Control crtl = Page.LoadControl("CheckOut.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("aboutus"))
                {
                    Control crtl = Page.LoadControl("AboutUs.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
                else if (subpara.Equals("myorder"))
                {
                    Control crtl = Page.LoadControl("MyOrder.ascx");
                    PlaceHolderUser.Controls.Add(crtl);
                }
            }
            else
            {
                Control crt = Page.LoadControl("NewProduct.ascx");
                PlaceHolderUser.Controls.Add(crt);
            }
            Load_dtlTopSeller();
        //}
    }
    protected void dtlProductType_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        HyperLink hplProductType = (HyperLink)e.Item.FindControl("hplProductType");
        hplProductType.Text = ((DataRowView)e.Item.DataItem)["ProductTypeName"].ToString();
        hplProductType.NavigateUrl = "index.aspx?subpara=product&ProductTypeID=" + ((DataRowView)e.Item.DataItem)["ProductTypeID"].ToString();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {        
        Response.Redirect("index.aspx?subpara=search&ProductName=" + TextBox1.Text);        
    }
    
    public void Load_dtlTopSeller()
    {
        dtlTopSeller.DataSource = pProduct.getTopSeller();
        dtlTopSeller.DataBind();
    }
    protected void dtlTopSeller_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        ImageButton ibtProductImage = (ImageButton)e.Item.FindControl("ibtProductImage");        
        Label lblProductName = (Label)e.Item.FindControl("lblProductName");
        Label lblProductPrice = (Label)e.Item.FindControl("lblProductPrice");
        Label lblSold = (Label)e.Item.FindControl("lblSold");
        Label lblProductQuantity = (Label)e.Item.FindControl("lblProductQuantity");
        HyperLink hplDetail = (HyperLink)e.Item.FindControl("hplDetail");
        DataRowView row = (DataRowView)e.Item.DataItem;
        lblProductName.Text = row["ProductName"].ToString();
        lblProductPrice.Text = row["ProductPrice"].ToString();
        lblSold.Text = row["Sold"].ToString();
        lblProductQuantity.Text = row["ProductQuantity"].ToString();
        ibtProductImage.ImageUrl = "Images/" + row["ProductImage"].ToString();
        hplDetail.NavigateUrl = "index.aspx?subpara=productdetail&ProductID=" + ((DataRowView)e.Item.DataItem)["ProductID"].ToString();
    }    
}
